﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities.DurableInstancing;
using System.Configuration;
using System.Xml.Linq;
using System.Runtime.DurableInstancing;

namespace Microhard.ApproveWorkflow.Impls
{
    public class SqlPersistenceStore
    {
        public static SqlWorkflowInstanceStore GetStore()
        {
            //装载sql cnn string
            string cnn = ConfigurationManager.ConnectionStrings["MicroHardApproveFlowConnectionString"].ConnectionString;
            SqlWorkflowInstanceStore ins = new SqlWorkflowInstanceStore(cnn);

            //当工作流执行完毕后，从数据库中删除流程实例。默认为DeleteAll.
            ins.InstanceCompletionAction = InstanceCompletionAction.DeleteAll;

            //声明，在wf实例上，需要存储附加信息（业务数据）
            //var dataNameSpace = XNamespace.Get("http://xhinker.com/");
            //List<XName> varProperties = new List<XName>();
            //varProperties.Add(dataNameSpace.GetName("BusinessSheetXName"));
            //ins.Promote("additionalPty", varProperties, null);

            //如果在多个host wf的环境里面，我们使用下面的代码，表示
            //每次加载同一个wf实例
            //InstanceHandle handle = ins.CreateInstanceHandle();
            //InstanceView view = ins.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(5));
            //handle.Free();
            //ins.DefaultInstanceOwner = view.InstanceOwner;

            return ins;
        }
    }
}
